<?php
/***************************************************************************
 * 
 * Copyright (c) 2010 , Inc. All Rights Reserved
 * $Id$:TestEncoding.php,2010/05/25 11:47:03 
 * 
 **************************************************************************/
 
 
 
/**
 * @file TestEncoding.php
 * @author huqingping
 * @date 2010/05/25 11:47:03
 * @version 1.0 
 * @brief 
 *  
 **/

final class TestEncoding extends TestCase 
{
	var $v = '';
	private function convert($data,$to)
	{
		if ($to == 'UTF-8') return $data;
		//*/
		if (is_string($data)) {
			$ret = mb_convert_encoding($data,$to,'UTF-8');
			return $ret;
		} else {
			foreach($data as $key=>$this->value)
			{
				$data[$key] = $this->convert($this->value,$to);
			}
			return $data;
		}
	}

	private function buildInput()
	{
		if (!$this->v) {
			$data = json_decode(file_get_contents('data'),true);
			$this->v = $data['data']['key'];
		}
		$get = array('k1'=>$this->v,'k2'=>$this->v,'_of'=>'default');
		$post = array('p1'=>$this->v,'p2'=>$this->v);
		$put = array('key1'=>$this->v,'key2'=>$this->v,'key3'=>$this->v);

		$input = array('get'=>$get,'post'=>$post,'put'=>$put);
		return $input;
	}

	private function getInput($encoding)
	{
		$input = $this->buildInput();
		$input = $this->convert($input,$encoding);
		$input['get']['_ie'] = $encoding;
		if (isset($input['put'])) {
			$putFile = '/tmp/HapN_put';
			file_put_contents($putFile, json_encode($input['put']));
			$input['put'] = $putFile;
		}
		return $input;
	}

	function go($from,$to)
	{
		$input = $this->getInput($from);
		$input['get']['_oe'] = $to;
		$ret = $this->fetch('/HapN/testEncoding',
			array('get'=>$input['get'],'post'=>$input['post']));
		$body = $ret->body;
		$this->assertTrue(strpos($body,$this->convert($this->v,$to)) > 0);
		$ret = $this->fetch('/HapN/testEncoding',
			array('get'=>$input['get'],'put'=>$input['put']));
		$body = $ret->body;
		$this->assertTrue(strpos($body,$this->convert($this->v,$to)) > 0);
	}

	function testUTF8_GBK()
	{
		$this->go('UTF-8','GBK');
	}

	function testGBK_UTF8()
	{
		$this->go('GBK','UTF-8');
	}

	function testGBK_GBK()
	{
		$this->go('GBK','GBK');
	}

	function testUTF8_UTF8()
	{
		$this->go('UTF-8','UTF-8');
	}

	function testUTF7_EUCCN()
	{
		$this->go('UTF-7','EUC-CN');
	}

	function testEUCCN_UTF8()
	{
		$this->go('EUC-CN','UTF-8');
	}

}




/* vim: set ts=4 sw=4 sts=4 tw=100 noet: */
?>
